package com.bph.schedule.algorithm;

import com.bph.commons.utils.SP;
import java.util.ArrayList;
import java.util.LinkedHashMap;

public class EmployeeFilter {

    /**
     * 根据当前班次信息和员工排班偏好对员工数据做一些“标记”，用于排班时参考
     * @param stage 当前班次
     * @param employees 员工数据
     * @return
     */
    public static ArrayList<LinkedHashMap<String,Object>> filterByTime
            (String stage,ArrayList<LinkedHashMap<String,Object>> employees){
        //解析班次信息
        String[] stages = SP.str_Parse(stage);
        double start = Double.parseDouble(stages[0]);
        double end = Double.parseDouble(stages[1]);
        double time = end-start; //班次时长
        //做标记
        for(LinkedHashMap<String,Object> employee:employees){
            double stage_degree = 0;//班次匹配度，越低越优先被筛掉
            double time_degree = 0;//时长匹配度，越低越优先被筛掉
            String pStage = String.valueOf(employee.get("pStage"));
            String[] pTime =SP.str_Parse(String.valueOf(employee.get("pTime")));
            double max_pTime = Double.parseDouble(pTime[1]);
            time_degree = max_pTime/time;
            employee.put("time_degree",time_degree);
            String[] arr_pStage = SP.str_Parse(pStage);
            double max_stage_degree = 0;
            for(int i=0;i+1<arr_pStage.length;i=i+2){
                double pStart = Double.parseDouble(arr_pStage[i]);
                double pEnd = Double.parseDouble(arr_pStage[i+1]);
                if(pStart>=start && pEnd<=end)
                    stage_degree = (pEnd-pStart)/time;
                else if(pStart>=start && pStart<=end && pEnd>end)
                    stage_degree = (end-pStart)/time;
                else if(pStart<start && pEnd>start && pEnd<=end)
                    stage_degree = (pEnd-start)/time;
                else if(pStart<=start && pEnd>=end)
                    stage_degree = 1;
                else
                    stage_degree = 0;
                if(stage_degree>max_stage_degree)
                    max_stage_degree = stage_degree;
            }
            stage_degree = max_stage_degree;
            employee.put("stage_degree",stage_degree);
        }
        return employees;
    }


    /**
     * 根据当前班次信息和员工排班情况对员工数据做一些“标记”，用于排班时参考
     * @param stage 当前班次
     * @param employees 员工数据
     * @return
     */
    public static ArrayList<LinkedHashMap<String,Object>> filterByStatus
    (String stage,ArrayList<LinkedHashMap<String,Object>> employees) {

        String[] stages = SP.str_Parse(stage);
        double start = Double.parseDouble(stages[0]);
        double end = Double.parseDouble(stages[1]);

        for (LinkedHashMap<String, Object> employee : employees) {
            int had_lunch = Integer.parseInt(String.valueOf(employee.get("had_lunch")));
            int had_supper = Integer.parseInt(String.valueOf(employee.get("had_supper")));
            int had_break = Integer.parseInt(String.valueOf(employee.get("had_break")));
            String[] dSchedule = SP.str_Parse(String.valueOf(employee.get("dSchedule")));
            double dayTotal = 0;
            int len = dSchedule.length;
            for(int i=0;i+1<len;i=i+2){
                dayTotal += Double.parseDouble(dSchedule[i+1])-Double.parseDouble(dSchedule[i]);
            }

            employee.put("lunch",0);
            employee.put("supper",0);
            employee.put("dayMax",0);
            employee.put("must_break",0);

            double lunch_start = Double.parseDouble(Data.lunchTime[0][0]);
            double lunch_end = Double.parseDouble(Data.lunchTime[0][1]);
            if(start>=lunch_start && start<lunch_end && had_lunch == 0){
                employee.put("lunch",1);//要吃午饭
                employee.put("had_lunch","1");//吃过了
            }

            double supper_start = Double.parseDouble(Data.supperTime[0][0]);
            double supper_end = Double.parseDouble(Data.supperTime[0][1]);
            if(start>=supper_start && start<supper_end && had_supper == 0){
                employee.put("supper",1);//要吃晚饭
                employee.put("had_supper","1");//吃过了
            }

            if(dayTotal>=Data.dayMax)
                employee.put("dayMax",1);

            if(Double.parseDouble(dSchedule[len-1])-Double.parseDouble(dSchedule[len-2])==Data.workMax && had_break==0){
                employee.put("must_break",1);//要休息
                employee.put("had_break","1");//休息过了
            }

        }
            return employees;
        }

}
